package users;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Scanner;

import dpssinterface.dpssInterface;


public class PlayerClient extends Thread {
		String serverName;
        BufferedWriter write ;
        FileWriter writer ;

	public static void main(String[] args) {
		int choice;
		try{
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			
		       Registry  registry= LocateRegistry.getRegistry(1099);
       
			dpssInterface NorthAmerica = (dpssInterface) registry.lookup("North");
			dpssInterface Europe = (dpssInterface) registry.lookup("Europe");
			dpssInterface Asia = (dpssInterface) registry.lookup("Asia");
			System.out.println("Let's Begin!");                        
			do
			{

				System.out.println("1. Create Game account from system");
                System.out.println("2. Sign In");
                System.out.println("3. Create new Game account ");
				System.out.println("4. Sign Out");
                System.out.println("5. Exit");
				choice = Integer.parseInt(br.readLine());
					
					switch(choice)
					{
					
					case 1:
						System.out.println("Your account has been created at each station");
						
						NorthAmerica.createPlayerAccount("Pratik","Joshi",24,"pratik","joshi","132.168.122.255");
						Europe.createPlayerAccount("Don","Danny",55,"don","danny","93.168.122.22");
						Asia.createPlayerAccount("Marie","Josh",23,"marie","josh","182.168.122.35");
						NorthAmerica.createPlayerAccount("James","Bond",33,"james","bond","132.168.122.255");
						Europe.createPlayerAccount("Alexandre","Hudon",25,"alex","hudon","93.168.122.22");
						Asia.createPlayerAccount("Winner","Wins",23,"winner","wins","182.168.122.35");
						NorthAmerica.createPlayerAccount("Kim","Jean",21,"kim","jean","132.168.122.255");
						Europe.createPlayerAccount("Me","You",55,"me","you","93.168.122.22");
						Asia.createPlayerAccount("Tom","Hanks",23,"tom","hanks","182.168.122.35");
						NorthAmerica.createPlayerAccount("James","Bond",33,"james","bond","132.168.122.255");
						NorthAmerica.playerSignIn("pratik","joshi","132.168.122.255");
						Europe.playerSignIn("don","danny","93.168.122.22");
						Asia.playerSignIn("marie","josh","182.168.122.35");
						break;
						
					
					case 2:
                        System.out.println("Enter the username and password : " );
                        Scanner sc = new Scanner(System.in);
                        String Username = sc.next();
                        String Password = sc.next();
                        System.out.println("Enter the IP Address : " );
                        String ipp = sc.next();
                        
                        String ans = "" ;
                        if(ipp.startsWith("132")){
                        	ans  = NorthAmerica.playerSignIn(Username, Password, ipp);
                             ans = ans + "From North America" ;
                   	
                        }else if(ipp.startsWith("93")){
                        	ans = Europe.playerSignIn(Username, Password, ipp);
                        	  ans = ans + "From Europe" ;
                        }else if(ipp.startsWith("182")){
                        	ans = Asia.playerSignIn(Username, Password, ipp);
                        	  ans = ans + "From Asia" ;
                        }
                      
                        System.out.println("Ans:"+ ans);
               		break;
						
                    case 3:
                        Scanner scanner = new Scanner(System.in);
                        System.out.println("Enter First Name: ");
                        String First = scanner.next();
                        System.out.println("Enter Last name: ");
                        String Last  = scanner.next() ;
                        System.out.println("Enter the Age: ");
                        int age = Integer.parseInt(scanner.next());
                        System.out.println("Enter the Username: ");
                        String usename = scanner.next();
                        if(usename.length() < 6 ) {
                        	System.out.println("Username Cannot be less than 6 characters. Please start again!");
                            break;
                        }
                        if (usename.length() > 15) {
                        	System.out.println("Username Cannot be more than 15 characters. Please start again!");
                            break;
                        }
                        	
                        System.out.println("Enter the Password: ");
                        String password = scanner.next();
                        if(password.length() < 6){
                        	System.out.println("Password Cannot be less than 6 characters. Please start again!");
                            break;
                        }
                        	
                        
                        System.out.println("Enter the  IP Address: ");
                        String scan = scanner.next();
                        
                      
                        if(scan.startsWith("132")){
                                 NorthAmerica.createPlayerAccount(First, Last, age, usename, password, scan);
                                 
                                   System.out.println("Account has been created for North America");
                        }else if(scan.startsWith("93")){
                       Europe.createPlayerAccount(First, Last, age, usename, password, scan);
                            
                        	System.out.println("Account has been created for Europe");
                        }else if(scan.startsWith("182")){
                         Asia.createPlayerAccount(First, Last, age, usename, password, scan);
                            
                        	System.out.println("Account has been created for Asia");
                        }
                        
                        else{
                        	System.out.println("Username already exist: Please try again with new Username.");
                        	
                        }
                         break;
						
					case 4: 
                           System.out.println("Enter the username and IPAddress" );
                           Scanner scc = new Scanner(System.in);
                           String username = scc.next();
                           String IP = scc.next();
                           String anse = NorthAmerica.playerSignOut(username, IP);
                           System.out.println("Ans:"+ anse);
                          break;
						
					case 5: 
						System.out.println("Cioa!");
						System.exit(0);
						break;
					
					default:
						System.out.println("Invalid selection :");
						break;
					}
					
				} while(choice != 5);
		}
		catch (Exception ex){
			ex.printStackTrace();
		}	
	}
}
